package com.education.platform.management.service.impl;


import com.education.platform.common.exception.BusinessException;
import com.education.platform.common.service.impl.BaseServiceImpl;
import com.education.platform.dao.ExaminPaperDao;
import com.education.platform.dao.QuestionBankDao;
import com.education.platform.dao.RelPaperQuestionDao;
import com.education.platform.dao.entity.ExaminPaperDO;
import com.education.platform.dao.entity.QuestionBankDO;
import com.education.platform.dao.entity.RelPaperQuestionDO;
import com.education.platform.dao.model.ExaminPaperModel;
import com.education.platform.management.config.ThreadUser;
import com.education.platform.management.push.PushUtil;
import com.education.platform.management.service.ExaminPaperService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * describe: 试卷
 *
 * @author :
 * @date: 2020-07-15 08:43:11.650
 **/
@Service
public class ExaminPaperServiceImpl extends BaseServiceImpl<ExaminPaperDao, ExaminPaperDO> implements ExaminPaperService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExaminPaperServiceImpl.class);

    @Autowired
    private ExaminPaperDao examinPaperDao;

    @Autowired
    private RelPaperQuestionDao relPaperQuestionDao;
    @Autowired
    private QuestionBankDao questionBankDao;
    @Autowired
    private PushUtil pushUtil;


    private final NumberFormat nf= NumberFormat.getNumberInstance() ;

    /**
     * 试卷列表
     * @param userId
     * @param gradeType
     * @return
     */
    @Override
    public List<ExaminPaperModel> getPaperList(Integer userId, String gradeType) {
        return examinPaperDao.getPaperList(userId, gradeType);
    }

    /**
     * 保存试卷
     * @param paperDO
     * @param questionIds
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(ExaminPaperDO paperDO,List<Integer> questionIds) {
        if(paperDO.getId() == null){
            paperDO.setCreateBy(ThreadUser.getUserName());
            paperDO.setCreateTime(new Date());
            paperDO.setDeleted(false);
            examinPaperDao.insert(paperDO);

//            //推送消息
//            if(Boolean.TRUE.equals(paperDO.getPush())){
////                PushUtil pushUtil = PushUtil.getInstance();
//                pushUtil.push( PushTypeEnum.PAPER, String.valueOf(paperDO.getId()), null);
//            }
        }else{
            paperDO.setUpdateBy(ThreadUser.getUserName());
            paperDO.setUpdateTime(new Date());
            examinPaperDao.updateById(paperDO);
        }
        saveRelPaperQuestion(paperDO.getId(),questionIds);
    }

    /**
     * 查询试卷
     * @param paperId
     * @param questionIds
     */
    private void saveRelPaperQuestion(Integer paperId,List<Integer> questionIds){
        if(paperId == null){
            throw new BusinessException("试卷id为空");
        }
        if(CollectionUtils.isEmpty(questionIds)){
            throw new BusinessException("试题为空");
        }
        relPaperQuestionDao.deleteByPaperId(paperId);
        List<RelPaperQuestionDO> list = new ArrayList<>(questionIds.size());
        RelPaperQuestionDO rel;
        for(Integer questionId:questionIds){
            rel  = new RelPaperQuestionDO();
            rel.setCreateBy(ThreadUser.getUserName());
            rel.setCreateTime(new Date());
            rel.setPaperId(paperId);
            rel.setQuestionId(questionId);
            list.add(rel);
        }
        relPaperQuestionDao.insertInBatch(list);
    }


    @Override
    public List<QuestionBankDO> getQuestionsByPaperId(Integer paperId) {
        return questionBankDao.getQuestionsByPaperId(paperId);
    }


}
